#include <algorithm>
#include <bitset>
#include <complex>
#include <cstddef>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
 
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
 
//leetcode代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;

     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};


TreeNode* CreateTree(vector<int>Data) {
	vector<string>str_data(Data.size(), "");
	for (int i = 0; i < Data.size(); i++) {
		if (Data[i] != NULL)
			str_data[i] = to_string(Data[i]);
		else
			str_data[i] = "null";
	}
	queue<TreeNode*>q;
	TreeNode* head = new TreeNode(stoi(str_data[0]));
	q.push(head);
	int i = 1;
	while (i < str_data.size()) {
		TreeNode* tmp = q.front();
		q.pop();
		if (str_data[i] != "null") {
			TreeNode* new_node = new TreeNode(stoi(str_data[i]));
			tmp->left = new_node;
			q.push(new_node);
		}
		i++;
		if (str_data[i] != "null") {
			TreeNode* new_node = new TreeNode(stoi(str_data[i]));
			tmp->right = new_node;
			q.push(new_node);
		}
		i++;
	}
	return head;
}


void prePrint(TreeNode* Root) {//先序遍历
	if (Root == NULL) return;
	cout << Root->val << ',';
	prePrint(Root->left);
	prePrint(Root->right);
	return;
}
void postPrint(TreeNode* Root) {//后序遍历
	if (Root == NULL) return;
	postPrint(Root->left);
	postPrint(Root->right);
	cout << Root->val << ',';
	return;
}
void inPrint(TreeNode* Root) {//中序遍历
	if (Root == NULL) return;
	inPrint(Root->left);
	cout << Root->val << ',';
	inPrint(Root->right);
	return;
}


class Solution {
public:
    bool compare(TreeNode* left, TreeNode* right){
        if(left ==nullptr && right !=nullptr) return false;
        else if(right == nullptr && left!= nullptr) return false;
        else if(right == nullptr && right == nullptr) return true;
        else if(right->val != left->val)    return false;
        else {
            return compare(left->left,right->right)&&
            compare(left->right,right->left);
        }
    }

    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        return compare(root->left,root->right);
    }
};
 
//主函数
int main(){  
    //在这里定义好函数中的测试数据
    // root = [1,2,2,3,4,4,3]
    // 输出：true
    vector<int>Data = { 1,2,3, NULL,4,5,6,7, NULL };
	TreeNode* Root = NULL;
	Root = CreateTree(Data);
	cout << "PreOrder:" << endl;
	prePrint(Root);
    cout << endl << "InOrder:" << endl;
	inPrint(Root);
	cout << endl << "PostOrder:" << endl;
	postPrint(Root);
    //root = [1,2,2,3,4,4,3];
    TreeNode* root_1 = NULL;
    vector<int>root_test_1 = { 1,2,2,3,4,4,3 };
    root_1 = CreateTree(root_test_1);
    cout << endl;
    Solution sol; // int i;
    cout << sol.isSymmetric(root_1) << endl;

    // root = [1,2,2,null,3,null,3]
    TreeNode* root_2 = NULL;
    vector<int>root_test_2 = { 1,2,2,NULL,3,NULL,3 };
    root_2 = CreateTree(root_test_2);
    cout << sol.isSymmetric(root_2) << endl;

	// ........

    return 0;    
}
